package wetalk.software.bupt.com.wetalk.util;

import java.util.Comparator;

import wetalk.software.bupt.com.wetalk.model.po.User;

/**
 * Created by Administrator on 2017/11/30.
 */

public class PinyinComparator implements Comparator{

    public PinyinComparator(){

    }

    @Override
    public int compare(Object o1, Object o2) {//首字母相同，正常比较；首字母不同，转为小写再比较
        User user1=(User)o1;
        User user2=(User)o2;
        MyPinyinParser pinyinParser=new MyPinyinParser();
        String py1=pinyinParser.toComplexPy(user1.getUserName());
        String py2=pinyinParser.toComplexPy(user2.getUserName());
        if(py1.charAt(0)<65 || (py1.charAt(0)>90 && py1.charAt(0)<97) || py1.charAt(0)>122 ){
            return 1;
        }
        if(py2.charAt(0)<65 || (py2.charAt(0)>90 && py2.charAt(0)<97) || py2.charAt(0)>122 ){
            return -1;
        }
        String smallPy1=py1.substring(0).toLowerCase();
        String smallPy2=py2.substring(0).toLowerCase();
        if(smallPy1.equals(smallPy2)){
            return py1.compareTo(py2);
        }else{
            return smallPy1.compareTo(smallPy2);
        }
    }

//    public static void main(String[] args) {
//
//        String[] names={"200","alibaba","zhangjie","123"};
//        List<User> list=new ArrayList<>();
//        for (int i = 0; i < names.length; i++) {
//            User user=new User();
//            user.setUserName(names[i]);
//            list.add(user);
//        }
//        for (User user: list) {
//            System.out.print(user.getUserName()+"\t");
//        }
//        System.out.println();
//        Collections.sort(list,new PinyinComparator());
//        for (User user: list) {
//            System.out.print(user.getUserName()+"\t");
//        }
//    }


}
